<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      > 

      <el-form-item label="环境名称" prop="environmentName">
          <el-input
            v-model="queryParams.environmentName"
            placeholder="请输入环境名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="创建者" prop="createBy">
          <el-input
            v-model="queryParams.createBy"
            placeholder="请输入创建者"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['project:environment:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['project:project:edit']"
            >修改</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['project:project:remove']"
            >删除</el-button>
          </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['project:project:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
  
      <el-table
        v-loading="loading"
        :data="elementUiList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="environmentId" :show-overflow-tooltip='true' />
        <el-table-column
          label="环境名称"
          align="center"
          prop="environmentName"
        />
        <el-table-column label="项目" align="center" prop="testplatfromProjectDescribe" :show-overflow-tooltip='true'/>
        <el-table-column label="创建者" align="center" prop="createBy" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['project:project:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['project:environment:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改项目管理对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="1500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="选择项目" prop="testplatfromProjectId" style="display: inline-block;">
            <el-select v-model="form.testplatfromProjectId" placeholder="请选择">
              <el-option
                v-for="item in selectProjectList"
                :key="item.testplatfromProjectId"
                :label="item.testplatfromProjectDescribe"
                :value="item.testplatfromProjectId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="环境名称" prop="environmentName" style="display: inline-block;">
            <el-input
              v-model="form.environmentName"
              placeholder="请输入环境名称"
            />
          </el-form-item>
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="通用配置" name="first">通用配置</el-tab-pane>

            <el-tab-pane label="HTTP配置" name="second">
              <!-- 环境域名 -->
              <el-form-item label="环境域名"  style="display: inline-block;">
                <el-input
                  v-model="testplatfromHttpconfiguration.environmentDomainName"
                  placeholder="例如https://www.csdn.net/"
                />
              </el-form-item>
              <!-- 描述 -->
              <el-form-item label="描述"  style="display: inline-block; ">
                <el-input
                  v-model="testplatfromHttpconfiguration.httpRemark"
                />
              </el-form-item>
              <!-- 启用条件 -->
              <el-form-item label="启用条件">
                <el-radio v-model="testplatfromHttpconfiguration.httpEbabling" label="NOTHAVE">无</el-radio>
                <el-radio v-model="testplatfromHttpconfiguration.httpEbabling" label="MODULE">模块</el-radio>
                <el-radio v-model="testplatfromHttpconfiguration.httpEbabling" label="ROUTE">路径</el-radio>
              </el-form-item>

            </el-tab-pane>

            <el-tab-pane label="数据库配置" name="third">数据库配置</el-tab-pane>
            <el-tab-pane label="全局断言" name="fourth">全局断言</el-tab-pane>
          </el-tabs>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
    
    <script>
  import {
    addProject,
    updateProject,
    listElement,
    addEnvironment,
    getEnvironment,
    delEnvironment,
    selectTestPlatfromProject
  } from "@/api/project/elementui";
  
  export default {
    name: "Project",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 项目管理表格数据
        elementUiList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //项链下拉框列表
        selectProjectList:[],
        //添加http配置
        testplatfromHttpconfiguration:{
          httpEbabling:"NOTHAVE",
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          environmentName: null,
          createBy: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          environmentName: [
            { required: true, message: "环境名称不能为空", trigger: "blur" },
            {
              min: 2,
              max: 20,
              message: "环境名称长度必须介于 2 和 20 之间",
              trigger: "blur",
            },
          ],
          testplatfromProjectId: [{ required: true, message: "请选择项目", trigger: "change" }],
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询项目管理列表 */
      getList() {
        this.loading = true;
        listElement(this.queryParams).then((response) => {
          console.log(response.data);
          this.elementUiList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          //环境id
          environmentId: null,
          //项目id
          testplatfromProjectId: null,
          //环境名称
          environmentName:null,
          //HTTP配置
          testplatfromHttpconfigurationList: [],
          //数据库连接配置
          testplatfromDatabaseList: [],
          //全局断言
          testplatfromAssertList: [],
          //通用配置
          testplatfromVariableList: [],
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.environmentId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.getProjectList(),
        this.open = true;
        this.title = "创建环境";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const environmentId = row.environmentId || this.ids;
        getEnvironment(environmentId).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改环境";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.environmentId != null) {
              updateProject(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                
                this.open = false;
                this.getList();
              });
            } else {
              addProject(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const environmentIds = row.environmentId;
        this.$modal
          .confirm(
            '是否确认删除环境编号为"' + environmentIds + '"的数据项？'
          )
          .then(function () {
            return delEnvironment(environmentIds);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "system/project/export",
          {
            ...this.queryParams,
          },
          `project_${new Date().getTime()}.xlsx`
        );
      },
      /**获取项目下拉列表 */
      getProjectList(){
        selectTestPlatfromProject().then((response) => {
          this.selectProjectList = response.data;
        });
      }
    },
  };
  </script>
    